
************************************************************************;
** Output Contract Level Frequent Used Procedures and Selected Rates  **;
** For 2005 HEDIS HMO dataset                                         **;
************************************************************************;

options ls=100 nofmterr ;

libname raw '/data/Medicare_P01_2009/data/HEDIS/rawdata';
libname new '/data/Medicare_P01_2009/data/HEDIS/sasdata' ;
libname plan '/data/Medicare_P01_2009/data/HEDIS/plandata';


data a0; set new.hedis2005;

   *************************************************;
   ** Demographic Variables and Sample Selection  **;
   *************************************************;
 
     if denom_flag=1;
     if state_code=' ' or state_code='00' or state_code>"53" or state_code in ("40","48") then delete;

     age=age+1; /* Set Age to end of year */

     if age>=65;

     if plan_type='HMO/HMOPOS' ;

     if age>65 and totmonth<12 then delete;

     death_dt=bene_death_dt;
     if (bene_valid_death_dt_sw='V' or death_dt>0) then death1=1; else death1=0;

     deathyear=year(death_dt);
     deathmon =month(death_dt);

     if death1=1 and
       (deathyear=2005 or
       (deathyear=2006 and (deathmon>0 and deathmon<3)))
     then delete ;

     length newagec $ 8;

     if age=65            then newagec='1.65';
     if age>65 and age<70 then newagec='2.66-69';
     if age>69 and age<75 then newagec='3.70-74';
     if age>74 and age<80 then newagec='4.75-79';
     if age>79 and age<85 then newagec='5.80-84';
     if age>84            then newagec='6.85+';      

     if racec=' ' then racec='0.Unknown';


    ******************************************;
    ** Recoding and Cleaning All Measures   **;
    ******************************************;

     ***** Quality Rates *****;

     array rate bcs_d bcs_n cmc_d cmc_n1 cdc_d1 cdc_n1 cdc_d3 cdc_n3 cdc_d4 cdc_n4 gso_d gso_n;
     do over rate;
        if rate=9 then rate=0;
     end;

             *-- cleaning BCS --*;

     if sex=1 or age>69 then bcs_d=.;
     if bcs_d=. or bcs_d=0 then bcs_n=.; 

             *-- cleaning CMC --*;

     if cmc_d=0 then cmc_n1=.; 
 
             *-- cleaning CDC --*;

     if age>75 then do; cdc_d1=.;  cdc_d2=.; cdc_d3=.; end;
     if cdc_d1=. or cdc_d1=0 then cdc_n1=.; 
     if cdc_d3=. or cdc_d3=0 then cdc_n3=.; 
     if cdc_d4=. or cdc_d4=0 then cdc_n4=.; 

             *-- cleaning GSO --*;

     if gso_d=0 then gso_n=.; 


     *****  Frequent Selected Procedures  *****;

     array uses fspcabgn fspptcan fsp_cc_n fsp_ce_n fsp_rffn fsp_thrn fsp_tkrn
                fsp_xlin fsp_oc_n fsp_clcn fsp_ah_n fsp_vh_n fsp_p_n fsp_masn fsp_lumn
                acd_n_t  acd_s_n  acd_m_n acdy_n_t acdy_s_n acdy_m_n
                ac_ov_n  ac_erv_n ac_asp_n ac_ors_n ;
     do over uses;
        if uses=999 then uses=0;
     end;

            *-- Gender specific Procedures --*;

     if sex=1 then do; fsp_ah_n=.; fsp_vh_n=.; fsp_masn=.; fsp_lumn=.; end;  /** female only **/
     if sex=2 then do; fsp_p_n=.;              end;  /** male only **/


     ***** CAP measures  John and Bruce email 6-4-2010 *****;

     if fspcabgn>3 then fspcabgn=3;   
     if fspptcan>4 then fspptcan=4;   
     if fsp_cc_n>4 then fsp_cc_n=4;   
     
     array fps2 fsp_ce_n fsp_rffn fsp_thrn fsp_tkrn fsp_xlin fsp_oc_n fsp_clcn fsp_ah_n fsp_vh_n fsp_p_n fsp_masn fsp_lumn;
     do over fps2; 
          if fps2>2 then fps2=2;  
     end;
       
     if acd_n_t > 12 then acd_n_t = 12;   * Total discharges set max 1 per month * ;    
     if acd_s_n > 12 then acd_s_n = 12;   * Surgery discharges *;
     if acd_m_n > 12 then acd_m_n = 12;   * Medical discharges *;

     if acdy_n_t > 365 then acdy_n_t = 365;  * Maximum possible in 12 months *;
     if acdy_s_n > 365 then acdy_s_n = 365;
     if acdy_m_n > 365 then acdy_m_n = 365;

     if ac_ov_n  > 50 then ac_ov_n  = 50; * Tot Out Patient visit set max 1 per week *;
     if ac_erv_n > 12 then ac_erv_n = 12; * Set max 1 per month *;
     if ac_asp_n > 12 then ac_asp_n = 12; 
     if ac_ors_n > 12 then ac_ors_n = 12;
 

     ********************************************************************;
     ** Rename All Variables and Will Keep Same Var Name for All Years **;
     ********************************************************************;

     n_cabg = fspcabgn ;  label n_cabg='CABG';
     n_ptca = fspptcan ;  label n_ptca='PTCA';
     n_cc   = fsp_cc_n ;  label n_cc  ='CardiacCathetherization';
     n_ce   = fsp_ce_n ;  label n_ce  ='CardiacEnarterectomy';
     n_rff  = fsp_rffn ;  label n_rff ='ReductionFemurFracture';
     n_thr  = fsp_thrn ;  label n_thr ='TotalHipReplacement';
     n_tkr  = fsp_tkrn ;  label n_tkr ='TotalKneereplacement';
     n_xli  = fsp_xlin ;  label n_xli ='ExcisionLargeIntest';
     n_oc   = fsp_oc_n ;  label n_oc  ='OpenCholecystectomy';
     n_clc  = fsp_clcn ;  label n_clc ='ClosedCholecstectomy';
     n_ah   = fsp_ah_n ;  label n_ah  ='AbdominalHysterectomy';
     n_vh   = fsp_vh_n ;  label n_vh  ='VaginalHysterectomy';
     n_p    = fsp_p_n ;   label n_p   ='Prostatectomy';
     n_mast = fsp_masn ;  label n_mast='Masterctomy' ;
     n_lump = fsp_lumn ;  label n_lump='Lumpectomy' ;


     iptotdis   = acd_n_t ;  label iptotdis ='InpatTotalDischarges';
     ipsurdis   = acd_s_n ;  label ipsurdis ='InpatSurgeryDischarges';
     ipmeddis   = acd_m_n ;  label ipmeddis ='InpatMedicineDischarges';

     iptotdays  = acdy_n_t ; label iptotdays ='InpatTotalDays';
     ipsurdays  = acdy_s_n ; label ipsurdays ='InpatSurgeryDays';
     ipmeddays  = acdy_m_n ; label ipmeddays ='InpatMedicineDays';

     amb_opv =  ac_ov_n ;   label amb_opv ='OutPatientVisits';
     amb_erv =  ac_erv_n ;  label amb_erv ='EmergencyRoomVisits';
     amb_asp =  ac_asp_n ;  label amb_asp ='AbmSugeryProcedures';
     amb_ors =  ac_ors_n ; label amb_ors ='ObservationRoomStays';


data a; set a0;

     %let all_vars = bcs_d bcs_n cmc_d cmc_n1 cdc_d1 cdc_d3 cdc_d4 cdc_n1 cdc_n3 cdc_n4 gso_d gso_n
                     n_cabg n_ptca n_cc n_ce n_rff n_thr n_tkr n_xli n_oc n_clc n_ah n_vh n_p n_mast n_lump
                     iptotdis ipsurdis ipmeddis iptotdays ipsurdays ipmeddays amb_opv amb_erv amb_asp amb_ors 
                     m_months;

     proc sort; by newagec gender racec state_code org_id bene_county_cd zip5;

     proc means data=a n sum noprint;
           by newagec gender racec state_code org_id bene_county_cd zip5;
               var &all_vars;
              output out=allst0 n=  sum= /autoname;

  data new.hedis05hmo; set allst0;

         if _TYPE_ = 0;
         tot_n = _FREQ_ ;
         totmon = m_months_sum;

         drop _TYPE_ ;
         drop _FREQ_ ;
         drop m_months_sum;
         drop m_months_n ;

         proc contents data=new.hedis05hmo varnum;
              title 'Content of HEDIS05hmo';

         proc print data=new.hedis05hmo(obs=5);

         proc freq;
              tables org_id;
endsas;
